import 'package:flutter/material.dart';
import 'package:flutter_demo/test/future_demo.dart';
import 'package:groovin_material_icons/groovin_material_icons.dart';
import 'package:flutter_demo/routers/application.dart';
import 'package:flutter_demo/routers/routers.dart';
import 'package:flutter_demo/views/app_home_go.dart';


///一个精美登录页面的实现，学习和熟悉flutter控件实例<br>
/// 注意ListView在横向布局是填满父控件的<br>
///
///
///
class PrettyLoginPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return PrettyLoginState();
  }
}

class PrettyLoginState extends State<PrettyLoginPage> {
  //注意这里 根的泛型是FormState,不是PrettyLoginState
  final _formKey = GlobalKey<FormState>();
  String _email;
  String _password;
  bool _isObscure = false;
  Color _eyeColor;

  //注意下面用法 List<Map>
  List<Map> _loginMethod = [
    {
      "title": "facebook",
      "icon": GroovinMaterialIcons.facebook,
    },
    {
      "title": "google",
      "icon": GroovinMaterialIcons.google,
    },
    {
      "title": "twitter",
      "icon": GroovinMaterialIcons.twitter,
    }
  ];

  Widget _buildTitle() => Text(
        'Login',
        style: TextStyle(color: Colors.black, fontSize: 24.0),
      );

  Widget _buildUnderTitleLine() => Padding(
        padding: EdgeInsets.only(top: 4.0),
        child: Align(
          alignment: Alignment.bottomLeft,
          child: Container(
            color: Colors.black,
            height: 2.0,
            width: 40.0,
          ),
        ),
      );

  Widget _buildEmailTextField() => TextFormField(
        decoration: InputDecoration(labelText: 'Emall Address'),
        onSaved: (value) => _email = value,
        validator: (value) {
          //对邮箱进行验证
          var emailReg = RegExp(
              r"[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?");
          if (!emailReg.hasMatch(value)) {
            return '请输入正确的邮箱';
          }
        },
      );

  Widget _buildPasswordTextField() => TextFormField(
        decoration: InputDecoration(
            labelText: 'Password',
            /**
             * 添加一个显示和溢出显示的按钮
             */
            suffixIcon: IconButton(
                icon: Icon(
                  Icons.remove_red_eye,
                  color: _eyeColor,
                ),
                onPressed: () {
                  setState(() {
                    _isObscure = !_isObscure;
                    _eyeColor = _isObscure
                        ? Colors.grey
                        : Theme.of(context).iconTheme.color;
                  });
                })),
        onSaved: (value) => _password = value,
        obscureText: _isObscure,
        validator: (value) {
          if (value.isEmpty) {
            return '请输入密码';
          }
        },
      );

  Widget _buildForgetPasswordText() => Padding(
        padding: EdgeInsets.only(top: 8),
        child: Align(
          alignment: Alignment.centerRight,
          child: FlatButton(
            child: Text('忘记密码?',
                style: TextStyle(fontSize: 14, color: Colors.grey)),
            onPressed: () {
              print('忘记密码');
            },
          ),
        ),
      );

  Widget _buildLogButton() => Align(
      alignment: Alignment.center,
      child: SizedBox(
        height: 45.0,
        width: 270.0,
        child: RaisedButton(
          child:
              Text('Login', style: Theme.of(context).primaryTextTheme.headline),
          color: Colors.black,
          onPressed: () {
            if (_formKey.currentState.validate()) {
              _formKey.currentState.save();
              print('email:$_email,password:$_password');
              Application.router.navigateTo(context, '${Routers.appHome}');
              Navigator.push(context, MaterialPageRoute(builder: (context) {
                return FutureDemo();
//                  return AppHomeGo();
              }));
            }
          },
        ),
      ));

  Widget _buildOtherLoginAccountText() => Align(
        alignment: Alignment.center,
        child: Text('其他账号登录', style: TextStyle(color: Colors.grey)),
      );

  ButtonBar _buildOtherMethod() => ButtonBar(
        alignment: MainAxisAlignment.center,
        children: _loginMethod
            .map((item) => Builder(
                  builder: (context) {
                    return IconButton(
                      icon: Icon(item['icon'],
                          color: Theme.of(context).iconTheme.color),
                      onPressed: () {
                        Scaffold.of(context).showSnackBar(SnackBar(
                          content: Text("${item['title']}登录"),
                          action: SnackBarAction(label: '取消', onPressed: () {}),
                        ));
                      },
                    );
                  },
                ))
            .toList(),
      );

  Align _buildRegisterText() => Align(
        alignment: Alignment.center,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text('没有账号?'),
            GestureDetector(
              child: Text(
                '点击注册',
                style: TextStyle(color: Colors.green),
              ),
              onTap: () {
                Scaffold.of(context).showSnackBar(SnackBar(
                  content: Text('跳转去注册'),
                  action: SnackBarAction(label: '取消', onPressed: () {}),
                ));
              },
            )
          ],
        ),
      );

  @override
  Widget build(BuildContext context) {
    return Form(
      //form表达可以做验证
      key: _formKey,
      child: ListView(
        //ListView中 横向布局是填满父控件的
        padding: EdgeInsets.symmetric(vertical: 24, horizontal: 24),
        children: <Widget>[
          _buildTitle(),
          _buildUnderTitleLine(),
          SizedBox(height: 70.0),
          _buildEmailTextField(),
          SizedBox(height: 30.0),
          _buildPasswordTextField(),
          _buildForgetPasswordText(),
          SizedBox(height: 40),
          _buildLogButton(),
          SizedBox(height: 20),
          _buildOtherLoginAccountText(),
          _buildOtherMethod(),
          _buildRegisterText(),
        ],
      ),
    );
  }
}
